package cn.itcast.web.controller.system;

import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.security.RolesAllowed;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/system/role",name = "角色管理")
public class RoleController extends BaseController {

    //依赖注入
    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/list",name = "分页查询")
    public String list(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "6")Integer pageSize){
        //调用方法
        PageInfo page = roleService.findByPage(pageNum, pageSize, getCompanyId());
        //存入域中
        request.setAttribute("page",page);
        //返回
        return "system/role/role-list";
    }


    //跳转新增页面
    @RequestMapping(value = "/toAdd",name = "跳转新增页面")
    public String toAdd(){
        return "system/role/role-add";
    }

    //新增方法
    @RequestMapping(value = "/edit",name = "新增/修改")
    public String edit(Role role){
        //根据id判断
        if (StrUtil.hasEmpty(role.getId())){
            //调用新增方法
            role.setCompanyId(getCompanyId());
            role.setCompanyName(getCompanyName());
            roleService.save(role);
        }else{
            //修改方法
            roleService.update(role);
        }
        //重定向到查询页面
        return"redirect:/system/role/list.do";
    }

    //跳转修改页面
    @RequestMapping(value = "/toUpdate",name = "跳转修改页面")
    public String toUpdate(String id){
        //查询当前角色
        Role byId = roleService.findById(id);
        //存入域中
        request.setAttribute("role",byId);
        //跳转页面
        return "system/role/role-update";
    }

    //删除
    @RequestMapping(value = "/delete",name = "删除")
    public String delete(String id){
        //获取方法
        roleService.delete(id);
        //重定向页面
        return "redirect:/system/role/list.do";
    }


    //权限管理
    @RequestMapping(value = "/roleModule",name = "进入模块分配页面")
    public String roleModule(String roleId){
        //调用方法
        Role role = roleService.findById(roleId);
        //存入域中
        request.setAttribute("role",role);
        //转发请求
        return "system/role/role-module";
    }


    @Autowired
    private ModuleService moduleService;

    //回显模块
    @RequestMapping(value = "/getZtreeNodes",name = "回显模块")
    @ResponseBody
    public List<Map> getZtreeNodes(String roleId){
        //创建集合
        List<Map> jsonList = new ArrayList<>();

        //查询出所有模块列表
        List<Module> moduleList = moduleService.findAll();

        //根据角色id查询中间表,获取模块的id列表
        List<String>  moduleIds = roleService.findModuleIdsByRoleId(roleId);

        //将模块列表转为json数组
        for (Module module : moduleList) {
            HashMap map = new HashMap();
            map.put("id",module.getId());
            map.put("pId",module.getParentId());
            map.put("name",module.getName());
            //判断如果当前角色选中包含此模块id,为jsp页面增加checked属性
            if (moduleIds.contains(module.getId())){
                map.put("checked",true);
            }
            //将map集合存入json中
            jsonList.add(map);
        }
        //返回json数据
        return jsonList;
    }


    @RequestMapping(value = "/updateRoleModule",name = "角色分配权限")
    public String updateRoleModule(String roleId,String[] moduleIds){
        //调用service中的方法
        roleService.updateRoleModule(roleId,moduleIds);
        //重定向页面
        return "redirect:/system/role/list.do";
    }

}
